package com.ihg.hiex.admin.web.controller.session.preparation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.account.AdministratorService;
import com.ihg.hiex.admin.service.session.ConferenceRoomService;
import com.ihg.hiex.admin.service.session.SessionMeetingService;
import com.ihg.hiex.admin.service.session.SessionService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.data.conference.ConferenceRoom;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.user.Administrator;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/sessions/{sessionId}/centerday-setting")
@MenuSetting("menu-sessions")
public class CenterDaySettingController {
    
    private final static Logger logger = LoggerFactory.getLogger(CenterDaySettingController.class);
    
    @Autowired
    private SessionService sessionService;
    
    @Autowired
    private SessionMeetingService sessionMeetingService;
    
    @Autowired
    private ConferenceRoomService conferenceRoomService;
    
    @Autowired
    private AdministratorService administratorService;
    
    /**
     * View center day setting index pages
     * 
     * @param login
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-preparation-centerday-setting")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(@PathVariable("sessionId") Long sessionId, @Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/centerday/centerday-setting");
        
        Session session = sessionService.load(sessionId);
        modelAndView.addObject("session", session);
        
        SessionMeeting meeting = sessionMeetingService.load(session, "Assessor Day 1 Opening");
        modelAndView.addObject("meeting", meeting);
        
        if (meeting != null) {
            
            // 1. 列出一个session下只有单个的meeting
            List<String> names = Arrays.asList("Assessor Day 1 Opening", "Delegate Day 1 Opening", "Midday Calibration", "Assessor Day 2 Opening", "Development Workshop", "Assessor Coaching Skills Recap");
            List<SessionMeeting> meetings = sessionMeetingService.list(session, names);
            Map<String, SessionMeeting> biuniqueMeetingMap = CollectionUtils.constructMapValue(meetings, "name");
            modelAndView.addObject("biuniqueMeetingMap", biuniqueMeetingMap);
            
            // 2. 列出一个session下有多个的meeting
            List<SessionMeeting> peerMeetings = sessionMeetingService.list(session, "Peer Calibration");
            modelAndView.addObject("peerMeetings", peerMeetings);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * create session meeting
     * 
     * @param sessionId
     * @return
     */
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public ModelAndView startMeeting(@PathVariable("sessionId") Long sessionId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/centerday/centerday-start");
        
        modelAndView.addObject("sessionId", sessionId);
        
        return modelAndView;
        
    }
    
    /**
     * on create meeting
     * 
     * @param sessionId
     * @return
     */
    @MenuSetting(value = "submenu-preparation-centerday-setting")
    @RequestMapping(value = "/start", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onStartMeeting(@PathVariable("sessionId") Long sessionId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            // 1. load current session
            Session session = sessionService.load(sessionId);
            
            // 2. create session meeting
            sessionMeetingService.create(session);
            
            httpResult.success().message("create success");
            
        } catch (Exception e) {
            logger.error("#onStartMeeting() - exception is [{}]", e);
            httpResult.fail("500").message("create fail");
        }
        
        return httpResult;
        
    }
    
    /**
     * edit meeting
     * 
     * @param sessionId
     * @param meetingId
     * @return
     */
    @RequestMapping(value = "/{meetingId}/edit", method = RequestMethod.GET)
    public ModelAndView editMeeting(@PathVariable("sessionId") Long sessionId,
                                    @PathVariable("meetingId") Long meetingId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/centerday/centerday-edit");
        
        // 1. load meeting 
        SessionMeeting meeting = sessionMeetingService.load(SessionMeeting.class, meetingId);
        modelAndView.addObject("meeting", meeting);
        
        // 2. list conference
        ConferenceRoom conferenceRoom = meeting.getConferenceRoom();
        List<ConferenceRoom> conferenceRooms = conferenceRoomService.list(conferenceRoom.getType());
        modelAndView.addObject("conferenceRooms", conferenceRooms);
        
        modelAndView.addObject("sessionId", sessionId);
        
        return modelAndView;
        
    }
    
    /**
     * on edit meeting
     * 
     * @param sessionId
     * @param meetingId
     * @param roomId
     * @return
     */
    @RequestMapping(value = "/{meetingId}", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditMeeting(@PathVariable("sessionId") Long sessionId,
                                                  @PathVariable("meetingId") Long meetingId,
                                                  @RequestParam("roomId") Long roomId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            SessionMeeting meeting = sessionMeetingService.load(SessionMeeting.class, meetingId);
            ConferenceRoom conferenceRoom = conferenceRoomService.load(ConferenceRoom.class, roomId);
            
            sessionMeetingService.update(meeting.edit(conferenceRoom));
            
            httpResult.success().message("edit success");
            
        } catch (Exception e) {
            logger.error("#onEditMeeting() - exception is [{}]", e);
            httpResult.fail("500").message("edit fail");
        }
        
        return httpResult;
        
    }
    
    /**
     * edit peer owner panel
     * 
     * @param sessionId
     * @param meetingId
     * @return
     */
    @RequestMapping(value = "/{meetingId}/edit-peer-owner", method = RequestMethod.GET)
    public ModelAndView editPeerOwner(@PathVariable("sessionId") Long sessionId,
                                      @PathVariable("meetingId") Long meetingId) {
        
        ModelAndView modelAndView = new ModelAndView("session/preparation/centerday/centerday-edit-peer");
        
        // 1. load session
        Session session = sessionService.load(Session.class, sessionId);
        modelAndView.addObject("session", session);
        
        // 2. load meeting
        SessionMeeting meeting = sessionMeetingService.load(SessionMeeting.class, meetingId);
        modelAndView.addObject("meeting", meeting);
        
        // 3. list administrator
        List<Administrator> administrators = administratorService.listAll(Administrator.class);
        modelAndView.addObject("administrators", administrators);
        
        return modelAndView;
        
    }
    
    /**
     * on edit peer owner
     * 
     * @param meetingId
     * @param meetingHostId
     * @return
     */
    @RequestMapping(value = "/{meetingId}/edit-peer-owner", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditPeerOwner(@PathVariable("meetingId") Long meetingId,
                                                    @RequestParam("meetingHostId") Long meetingHostId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            SessionMeeting meeting = sessionMeetingService.load(SessionMeeting.class, meetingId);
            Administrator meetingHost = administratorService.load(Administrator.class, meetingHostId);

            sessionMeetingService.update(meeting.edit(meetingHostId, meetingHost.getConferenceRoom()));
            
            httpResult.success().message("success");
            
        } catch (Exception e) {
            logger.error("#onEditPeerOwner() - ");
            httpResult.fail("500").message("failed");
        }
        
        return httpResult;
        
    }
    
}
